package cn.iocoder.yudao.module.shop.revenue.controller.app;

import cn.iocoder.yudao.framework.business.basic.annotation.AppJsonView;
import cn.iocoder.yudao.framework.business.basic.pojo.bo.ExtraPageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.revenue.enums.RevenueStatusEnum;
import cn.iocoder.yudao.module.shop.revenue.pojo.dto.RevenueQueryDTO;
import cn.iocoder.yudao.module.shop.revenue.pojo.vo.RevenueVO;
import cn.iocoder.yudao.module.shop.revenue.service.app.AppRevenueService;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 前端控制器
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/04/30
 */
@Tag(name = "用户 APP - 商家营业收益")
@RestController
@RequestMapping("/shop/revenue")
public class AppRevenueController {
    @Resource
    private AppRevenueService appRevenueService;

    @Resource
    private ShopInfoApi shopInfoApi;

    @Operation(summary = "分页")
    @Parameter(name = "pageNo", description = "页码", required = true, example = "1")
    @Parameter(name = "pageSize", description = "每页数据条数", required = true, example = "10")
    @GetMapping("/page")
    @JsonView(AppJsonView.class)
    public CommonResult<ExtraPageResult<RevenueVO>> getPage(Integer pageNo, Integer pageSize, RevenueQueryDTO param) {
        param.setShopId(getShopId());
        return toExtraPageResult(pageNo, pageSize, param);
    }

    @Operation(summary = "分页（今日收益）", deprecated = true, description = "直接使用 page 接口。今日待入账及已结算的收益")
    @Parameter(name = "pageNo", description = "页码", required = true, example = "1")
    @Parameter(name = "pageSize", description = "每页数据条数", required = true, example = "10")
    @GetMapping("/page-today")
    @JsonView(AppJsonView.class)
    @Deprecated
    public CommonResult<ExtraPageResult<RevenueVO>> getTodayPage(Integer pageNo, Integer pageSize) {
        RevenueQueryDTO param = new RevenueQueryDTO();
        param.setShopId(getShopId());
        LocalDateTime now = LocalDateTime.now();
        param.setYear(now.getYear());
        param.setMonth(now.getMonthValue());
        param.setDay(now.getDayOfMonth());
        return toExtraPageResult(pageNo, pageSize, param);
    }

    @Operation(summary = "分页（待入账收益）", deprecated = true, description = "直接使用 page 接口。累计待入账的收益")
    @Parameter(name = "pageNo", description = "页码", required = true, example = "1")
    @Parameter(name = "pageSize", description = "每页数据条数", required = true, example = "10")
    @GetMapping("/page-freezing")
    @JsonView(AppJsonView.class)
    @Deprecated
    public CommonResult<ExtraPageResult<RevenueVO>> getFreezingPage(Integer pageNo, Integer pageSize) {
        RevenueQueryDTO param = new RevenueQueryDTO();
        param.setShopId(getShopId());
        param.setStatus(RevenueStatusEnum.FREEZING.getValue());
        return toExtraPageResult(pageNo, pageSize, param);
    }

    @Operation(summary = "分页（累计总收益）", deprecated = true, description = "直接使用 page 接口。累计已结算的收益")
    @Parameter(name = "pageNo", description = "页码", required = true, example = "1")
    @Parameter(name = "pageSize", description = "每页数据条数", required = true, example = "10")
    @GetMapping("/page-total")
    @JsonView(AppJsonView.class)
    @Deprecated
    public CommonResult<ExtraPageResult<RevenueVO>> getTotalPage(Integer pageNo, Integer pageSize) {
        RevenueQueryDTO param = new RevenueQueryDTO();
        param.setShopId(getShopId());
        param.setStatus(RevenueStatusEnum.UNFREEZE.getValue());
        return toExtraPageResult(pageNo, pageSize, param);
    }

    private Long getShopId() {
        return shopInfoApi.getShopIdByMemberUserId(getLoginUserId());
    }

    private CommonResult<ExtraPageResult<RevenueVO>> toExtraPageResult(Integer pageNo, Integer pageSize, RevenueQueryDTO param) {
        PageResult<RevenueVO> page = appRevenueService.getPage(pageNo, pageSize, param);
        ExtraPageResult<RevenueVO> pageX = new ExtraPageResult<>(page.getList(), page.getTotal());
        pageX.put("numberSum", MoneyUtils.fenToYuanStr(appRevenueService.getNumberSum(param)));
        return CommonResult.success(pageX);
    }

}
